package com.mjsoftking.paletteapp;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.alibaba.fastjson.JSON;
import com.mjsoftking.paletteapp.databinding.ActivityMainBinding;

import java.io.IOException;
import java.util.Locale;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.setClick(this);

        binding.handView.palette.setPaintColor(R.color.purple_200);
        binding.handView.palette.setEnableOnClick(true);
        binding.handView.palette.setEnableEndToEnd(false);
        binding.handView.palette.setEnableLine(true);
        binding.handView.palette.setEnableOnce(false);
        binding.handView.palette.setEnableDraw(true);

        binding.handView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.w("TAG", "binding.handView Click");
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v.equals(binding.revocation)) {
            binding.handView.palette.revocation();
        } else if (v.equals(binding.clear)) {
            binding.handView.palette.clear();
        } else if (v.equals(binding.look)) {
//            binding.handView.getLines();
            Toast.makeText(this,
                    JSON.toJSONString(binding.handView.palette.getLines()),
                    Toast.LENGTH_LONG).show();
        } else if (v.equals(binding.save)) {
            try {
                String fileName = binding.handView.palette.saveBitmap();
                Toast.makeText(this,
                        String.format(Locale.getDefault(), "已保存到“%s”", fileName),
                        Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}